package com.zch.config;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zch.enums.DataSourceEnum;
import com.zch.multiple.MultipleDataSource;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
 * The type Mybatis plus configuration.
 *
 * @author zchcpywin10
 */
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "com.zch.mapper")
public class MybatisPlusConfiguration {
  /**
   * Db 1 data source.
   *
   * @return the data source
   */
  @Bean(name = "db1")
  @ConfigurationProperties(prefix = "spring.datasource.druid.db1")
  public DataSource db1() {
    return DruidDataSourceBuilder.create().build();
  }
  /**
   * Db 2 data source.
   *
   * @return the data source
   */
  @Bean(name = "db2")
  @ConfigurationProperties(prefix = "spring.datasource.druid.db2")
  public DataSource db2() {
    return DruidDataSourceBuilder.create().build();
  }
  /**
   * Db 3 data source.
   *
   * @return the data source
   */
  @Bean(name = "db3")
  @ConfigurationProperties(prefix = "spring.datasource.druid.db3")
  public DataSource db3() {
    return DruidDataSourceBuilder.create().build();
  }
  /**
   * 动态数据源配置
   *
   * @param db1 the db 1
   * @param db2 the db 2
   * @param db3 the db 3
   * @return data source
   */
  @Bean
  @Primary
  public DataSource multipleDataSource(@Qualifier("db1") DataSource db1, @Qualifier("db2") DataSource db2, @Qualifier("db3") DataSource db3) {
    MultipleDataSource multipleDataSource = new MultipleDataSource();
    Map<Object, Object> targetDataSources = new HashMap<>();
    targetDataSources.put(DataSourceEnum.DB1.getValue(), db1);
    targetDataSources.put(DataSourceEnum.DB2.getValue(), db2);
    targetDataSources.put(DataSourceEnum.DB3.getValue(), db3);
    //添加数据源
    multipleDataSource.setTargetDataSources(targetDataSources);
    //设置默认数据源
    multipleDataSource.setDefaultTargetDataSource(db1);
    return multipleDataSource;
  }
  /**
   * Sql session factory sql session factory.
   *
   * @return the sql session factory
   * @throws Exception the exception
   */
  @Bean("sqlSessionFactory")
  public SqlSessionFactory sqlSessionFactory() throws Exception {
    MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
    sqlSessionFactory.setDataSource(multipleDataSource(db1(), db2(), db3()));
    //添加分页插件
    //    sqlSessionFactory.setPlugins(new Interceptor[]{paginationInterceptor()})
    sqlSessionFactory.setPlugins(mybatisPlusInterceptor());
    // 扫描mybatis文件
    // sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mappers/*.xml"))
    sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*Mapper.xml"));
    return sqlSessionFactory.getObject();
  }
  /**
   * Mybatis plus interceptor mybatis plus interceptor.
   *
   * @return the mybatis plus interceptor
   */
  @Bean
  public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE));
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    return interceptor;
  }
  /**
   * 自动识别使用的数据库类型
   * 在mapper.xml中databaseId的值就是跟这里对应，
   * 如果没有databaseId选择则说明该sql适用所有数据库
   */
  @Bean
  public DatabaseIdProvider getDatabaseIdProvider() {
    DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
    Properties properties = new Properties();
    properties.setProperty("Oracle", "oracle");
    properties.setProperty("MySQL", "mysql");
    properties.setProperty("DB2", "db2");
    properties.setProperty("Derby", "derby");
    properties.setProperty("H2", "h2");
    properties.setProperty("HSQL", "hsql");
    properties.setProperty("Informix", "informix");
    properties.setProperty("MS-SQL", "ms-sql");
    properties.setProperty("PostgreSQL", "postgresql");
    properties.setProperty("Sybase", "sybase");
    properties.setProperty("Hana", "hana");
    databaseIdProvider.setProperties(properties);
    return databaseIdProvider;
  }
}
//  /**
//   * 分页插件，自动识别数据库类型
//   */
//  @Bean
//  public PaginationInterceptor paginationInterceptor() {
//    PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
//    // 设置请求的页面大于最大页后操作， true调回到首页，false 继续请求  默认false
//    // paginationInterceptor.setOverflow(false);
//    // 设置最大单页限制数量，默认 500 条，-1 不受限制
//    // paginationInterceptor.setLimit(500);
//    return paginationInterceptor;
//  }
